package com.chainyoung.component_tmperson.ui.mvp.model.dao;

import android.util.Pair;

import com.chainyoung.common.utils.RxRealmUtils;
import com.chainyoung.component_tmperson.entity.BrowseCache;

import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.FlowableEmitter;
import io.reactivex.functions.Consumer;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmResults;

public class BrowseDAO {

    private RealmConfiguration realmConfiguration;

    public BrowseDAO(RealmConfiguration realmConfiguration) {
        this.realmConfiguration = realmConfiguration;
    }


    public Flowable<BrowseCache> addBrowseCache(final BrowseCache item) {
        return RxRealmUtils
                .flowableExec(realmConfiguration, new Consumer<Pair<FlowableEmitter, Realm>>() {
                    @Override
                    public void accept(final Pair<FlowableEmitter, Realm> pair) throws Exception {
                        pair.second.executeTransaction(new Realm.Transaction() {
                            @Override
                            public void execute(Realm r) {
                                pair.first.onNext(r.copyFromRealm(r.copyToRealmOrUpdate(item)));
                                pair.first.onComplete();
                            }
                        });
                    }
                });
    }


    public Flowable<List<BrowseCache>> getBrowseCaches() {
        try (final Realm realm = Realm.getInstance(realmConfiguration)) {
            RealmResults<BrowseCache> queryResult = realm.where(BrowseCache.class)
                    .findAll();
            if (queryResult != null) {
                return Flowable.just(realm.copyFromRealm(queryResult));
            } else {
                return Flowable.empty();
            }
        }
    }

    public Flowable<Boolean> deleteScheduleCacheByUrl(final String time, final String url) {
        return RxRealmUtils
                .flowableExec(realmConfiguration, new Consumer<Pair<FlowableEmitter, Realm>>() {
                    @Override
                    public void accept(final Pair<FlowableEmitter, Realm> pair) throws Exception {
                        pair.second.executeTransactionAsync(new Realm.Transaction() {
                            @Override
                            public void execute(Realm r) {
                                final boolean isSuccess = r.where(BrowseCache.class)
                                        .equalTo("updtime", time)
                                        .equalTo("url", url)
                                        .findAll()
                                        .deleteAllFromRealm();
                                pair.first.onNext(isSuccess);
                                pair.first.onComplete();
                            }
                        });
                    }
                });
    }


    public Flowable<Boolean> deleteScheduleCacheAll() {
        return RxRealmUtils
                .flowableExec(realmConfiguration, new Consumer<Pair<FlowableEmitter, Realm>>() {
                    @Override
                    public void accept(final Pair<FlowableEmitter, Realm> pair) throws Exception {
                        pair.second.executeTransactionAsync(new Realm.Transaction() {
                            @Override
                            public void execute(Realm r) {
                                final boolean isSuccess = r.where(BrowseCache.class)
                                        .findAll()
                                        .deleteAllFromRealm();
                                pair.first.onNext(isSuccess);
                                pair.first.onComplete();
                            }
                        });
                    }
                });
    }


}
